package org.youdian.rainsound;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;
import android.widget.ImageView;


public class Main extends Activity {
	private MediaPlayer mplayer;
	private ImageView iView;
	private Drawable rainy ;
	//声明图片ID
	private int [] fileid ={
			R.drawable.rain0,R.drawable.rain1,R.drawable.rain2,R.drawable.rain3,R.drawable.rain4,
			R.drawable.rain5,R.drawable.rain6,R.drawable.rain7,R.drawable.rain8,R.drawable.rain9,
			R.drawable.rain10,R.drawable.rain11,R.drawable.rain12,R.drawable.rain13,R.drawable.rain14,
			R.drawable.rain15,R.drawable.rain16,R.drawable.rain17,R.drawable.rain18,R.drawable.rain19,
			R.drawable.rain20,R.drawable.rain21,R.drawable.rain22,R.drawable.rain23,R.drawable.rain24,
			R.drawable.rain25,R.drawable.rain26,R.drawable.rain27,R.drawable.rain28,R.drawable.rain29,
			R.drawable.rain30,R.drawable.rain31,R.drawable.rain32,R.drawable.rain33,R.drawable.rain34,
			R.drawable.rain35
			};
    Resources rs;
    //获取系统resources
	Resources getRs(){
		return Main.this.getResources(); 
	}
	
	    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        iView=(ImageView)findViewById(R.id.iView);
        mplayer=MediaPlayer.create(this, R.raw.rainsound2);
        mplayer.setLooping(true);
        if(!mplayer.isPlaying())
        	mplayer.start();
        //点击ImageView的时候播放/暂停音乐
        iView.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(mplayer.isPlaying()) 
					mplayer.pause();
				else
					mplayer.start();
				
			}
        	
        });

        handler.post(update);
        
    }
        
        Runnable update =new Runnable(){
            
        	int i=0;
        	//通过更新图片来实现下雨的动画效果
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if(i==35)i=0;
				Log.v("aaa", "i");
				
				//fileid=Integer.parseInt(filename);
				 rs=getRs();
				rainy=rs.getDrawable(fileid[i]);
				iView.setBackgroundDrawable(rainy);
				i++;
				handler.postDelayed(update, 300);
			}
        	
        };   
       
        Handler handler=new Handler();
		@Override
		protected void onStop() {
			// TODO Auto-generated method stub
			super.onStop();
			mplayer.stop();
			mplayer.release();
			handler.removeCallbacks(update);
			this.finish();
		}
        
           
              
}    
   
